import FScene from "../Libs/Framework/Support/FScene";
import { WebManager } from "../Libs/Framework/Managers/WebManager";
import { Fun } from "../Libs/Framework/Utility/dx/Fun";
import { Application } from "../Libs/Framework/Support/Application";
import { GameDataManager } from "../Libs/Framework/Managers/GameDataManager";
import CommonID from "../Libs/NetWork/Common/CommonID";
import { EventManager } from "../Libs/Framework/Managers/EventManager";
import { GameEvent, SceneName } from "../Libs/Framework/Event/GameEvent";
import { SceneEvent } from "../Libs/Framework/Event/SceneEvent";
import { ModuleEvent } from "../Libs/Framework/Event/ModuleEvent";
import { ModuleNames } from "../Libs/Framework/Enums/ModuleNames";
import { GameLayer } from "../Libs/Framework/Enums/GameLayer";
import { Console } from "../Libs/Utils/Console";

const { ccclass, property } = cc._decorator;
@ccclass
export default class LoginScene extends FScene {
    @property(cc.EditBox)
    private wxCode : cc.EditBox = null

    public start() : void {
        Console.instace().err("嘿嘿红色测试")
    }

    private Login () : void {
        WebManager.GetWebData("?token=" + this.wxCode.string, "", Fun(this.LoginSuccess, this), Fun(this.LoginFail,this));
    }

    public addEvents() : void {
        EventManager.addEvent(this, GameEvent.ON_LOGIN_SUCCESS, this.ChangeScene);
    }

    private LoginSuccess(data:login):void{
        if(data.code != 1){
            
            console.log("登录失败",data.msg)
        }
        else{
            //登录成功后的初始化数据
            GameDataManager.game.playerId = data.uid
            Application.Mediator.Proxy = new CommonID
            Application.Mediator.ConnectService = data.server
        }
    }

    private LoginFail():void{
        EventManager.dispatchModuleEventWith(ModuleEvent.SHOW_MODULE, ModuleNames.Confirm, GameLayer.Window,{
            content:"请求服务器数据失败",
            Confirm:()=>{
                EventManager.dispatchModuleEventWith(ModuleEvent.DISPOSE_MODULE, ModuleNames.Confirm);
            }
        });
    }

    private ChangeScene() : void {
        EventManager.dispatchEvent(new SceneEvent(SceneEvent.CHANGE_SCENE, SceneName.LOBBY_SCENE,{}));
    }

}


interface login {
    code: number, 
    msg: string,
    uid:number,
    server : string
}